package com.example.administrator.yuejian23.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.yuejian23.Bean.OrderInfo;
import com.example.administrator.yuejian23.Const;
import com.example.administrator.yuejian23.R;
import com.example.administrator.yuejian23.Retrofit.RetrofitService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/11/12.
 */

public class OrderDetailActivity  extends AppCompatActivity{
    private TextView orderinfo_name, orderinfo_time;
    private TextView       orderinfo_place, orderinfo_adultnum, orderinfo_paytype;
    private TextView      orderinfo_vtype, payprice, orderinfo_phone, danjia;
    private ImageView orderinfo_pic, orderinfo_back;
    private Button orderinfo_tuikuan;
    private String  str_buynum, user_name;
    private String order_id, order_type;
    private OrderInfo orderInfo;
    private Double price, allprice, buynum;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_order_info);
        orderinfo_name = (TextView) findViewById(R.id.orderinfo_name);
        orderinfo_time = (TextView) findViewById(R.id.orderinfo_time);
        orderinfo_place = (TextView) findViewById(R.id.orderinfo_place);
        orderinfo_adultnum = (TextView) findViewById(R.id.orderinfo_adultnum);
        orderinfo_paytype = (TextView) findViewById(R.id.orderinfo_paytype);
        orderinfo_vtype = (TextView) findViewById(R.id.orderinfo_vtype);
        orderinfo_phone = (TextView) findViewById(R.id.orderinfo_phone);
        danjia = (TextView) findViewById(R.id.orderinfo_danjia);
        payprice = (TextView) findViewById(R.id.payprice);
        orderinfo_pic = (ImageView) findViewById(R.id.orderinfo_pic);
        orderinfo_back = (ImageView) findViewById(R.id.orderinfo_back);
        orderinfo_tuikuan = (Button) findViewById(R.id.orderinfo_tuikuan);
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("username","0");
        Intent intent = getIntent();
        order_id=intent.getStringExtra("order_id");
        order_type = intent.getStringExtra("order_type");
        price = Double.valueOf(intent.getStringExtra("price"));
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService services=retrofit.create(RetrofitService.class);
        retrofit2.Call<OrderInfo> call=services.getOrderInfo(order_id,order_type);
        call.enqueue(new Callback<OrderInfo>() {
            @Override
            public void onResponse(Call<OrderInfo> call, Response<OrderInfo> response) {
                orderInfo = response.body();
                orderinfo_name.setText(orderInfo.getName());
                orderinfo_place.setText(orderInfo.getPlace());
                orderinfo_time.setText(orderInfo.getTraintime());
                orderinfo_adultnum.setText(orderInfo.getAdultnum());
                orderinfo_phone.setText(user_name);
                danjia.setText(orderInfo.getPrice());
                str_buynum = orderInfo.getAdultnum().toString();
                buynum = Double.parseDouble(str_buynum);
                allprice = price*buynum;
                payprice.setText(allprice.toString()+"元");
                if (orderInfo.getPaytype()=="1"){
                    orderinfo_paytype.setText("支付宝支付");
                }
                if (orderInfo.getPaytype()=="2"){
                    orderinfo_paytype.setText("微信支付");
                }
                if (orderInfo.getVstate()=="0"){
                    orderinfo_vtype.setText("未参加");
                }
                if (orderInfo.getVstate()=="1"){
                    orderinfo_vtype.setText("已完成");
                }
                if (orderInfo.getThumb()==null){
                    orderinfo_pic.setImageResource(R.mipmap.ic_start);
                }
                else {
                    String address="http://172.24.10.175/workout/Uploads/"+orderInfo.getThumb();
                    Picasso.with(mContext).load(address).into(orderinfo_pic);
                }

            }

            @Override
            public void onFailure(Call<OrderInfo> call, Throwable t) {

            }
        });
        orderinfo_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        orderinfo_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }




}
